package com.demo.service;

import java.util.ArrayList;
import java.util.List;

import com.demo.common.model.Book;
import com.demo.common.model.Comments;
import com.demo.common.model.ExchangeItem;
import com.demo.common.model.ExchangeTrade;
import com.demo.common.model.Order;
import com.demo.common.model.OrderItem;
import com.demo.common.model.SecondTrade;
import com.demo.common.model.User;
import com.demo.constants.Constants;
import com.jfinal.plugin.activerecord.Page;

public class CommentsService {

	private static final Comments dao = new Comments().dao();

	public Page<Comments> mine(int pageNumber, int pageSize, User suser) {
		Page<Comments> page = dao
				.paginate(pageNumber, pageSize, "select *",
						"from comments where user_id=? order by id desc",
						suser.getId());
		for (Comments comm : page.getList()) {
			comm.setBook(Book.dao.findById(comm.getBookId()));
		}
		return page;
	}

	public Page<Comments> getByBookId(int pageNumber, int pageSize,
			Integer bookId) {
		Page<Comments> page = dao.paginate(pageNumber, pageSize, "select *",
				"from comments where book_id=? and status=1 order by id desc",
				bookId);
		return page;
	}

	public Page<Comments> getAuditList(int pageNumber, int pageSize,
			Integer status) {
		Page<Comments> page = dao.paginate(pageNumber, pageSize, "select *",
				"from comments where status=? order by id desc", status);
		for (Comments comm : page.getList()) {
			comm.setBook(Book.dao.findById(comm.getBookId()));
		}
		return page;
	}

	public void audit(Integer id, int status) {
		Comments comments = dao.findById(id);
		comments.setStatus(status);
		comments.update();
	}

	public List<Book> getBooks(String orderId, User suser) {
		List<Book> books = new ArrayList<Book>();
		if (orderId.startsWith("O")) {
			List<OrderItem> list = OrderItem.dao.find(
					"select * from order_item where order_id=?", orderId);
			for (OrderItem item : list) {
				books.add(Book.dao.findById(item.getBookId()));
			}
		} else if (orderId.startsWith("S")) {
			SecondTrade trade = SecondTrade.dao.findById(orderId);
			books.add(Book.dao.findById(trade.getBookId()));
		} else {
			ExchangeTrade eTrade = ExchangeTrade.dao.findById(orderId);
			ExchangeItem userExchangeItem = ExchangeItem.dao.findById(eTrade
					.getUserExchangeItemId());
			ExchangeItem buyerExchangeItem = ExchangeItem.dao.findById(eTrade
					.getBuyerExchangeItemId());
			if (eTrade.getUserId() == suser.getId()) {
				books.add(Book.dao.findById(buyerExchangeItem.getBookId()));
			} else if (eTrade.getBuyerId() == suser.getId()) {
				books.add(Book.dao.findById(userExchangeItem.getBookId()));
			}
		}
		return books;
	}

	public void updateCommented(String orderId, User suser) {
		if (orderId.startsWith("O")) {
			Order order = Order.dao.findById(orderId);
			order.setCommented(Constants.order_comments_yes);
			order.update();
		} else if (orderId.startsWith("S")) {
			SecondTrade trade = SecondTrade.dao.findById(orderId);
			trade.setCommented(Constants.order_comments_yes);
			trade.update();
		} else {
			ExchangeTrade eTrade = ExchangeTrade.dao.findById(orderId);
			ExchangeItem userExchangeItem = ExchangeItem.dao.findById(eTrade
					.getUserExchangeItemId());
			ExchangeItem buyerExchangeItem = ExchangeItem.dao.findById(eTrade
					.getBuyerExchangeItemId());
			if (eTrade.getUserId() == suser.getId()) {
				buyerExchangeItem.setCommented(Constants.order_comments_yes);
				buyerExchangeItem.update();
			} else if (eTrade.getBuyerId() == suser.getId()) {
				userExchangeItem.setCommented(Constants.order_comments_yes);
				userExchangeItem.update();
			}
		}

	}

}
